package com.houtai.authority;


import com.alibaba.fastjson.JSONObject;


import com.houtai.bean.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * 权限认证拦截器
 */
public class AuthorityAnnotationInterceptor extends HandlerInterceptorAdapter {
	private Logger logger = LoggerFactory.getLogger(AuthorityAnnotationInterceptor.class);
	private RedisTemplate redisTemplate;

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// 请求处理完成之后
		super.afterCompletion(request, response, handler, ex);
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
						   ModelAndView modelAndView) throws Exception {
		// 处理器执行完毕之后
		super.postHandle(request, response, handler, modelAndView);
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		if (handler instanceof HandlerMethod) {
			HandlerMethod hm = (HandlerMethod) handler;

			Class<?> clazz = hm.getBeanType();
			Method m = hm.getMethod();
			try {
				response.setContentType("text/html;charset=UTF-8");
				JSONObject responseMap = new JSONObject();
				if (clazz != null && m != null) {
					boolean isClzAnnotation = clazz.isAnnotationPresent(com.houtai.authority.Authority.class);
					boolean isMethondAnnotation = m.isAnnotationPresent(com.houtai.authority.Authority.class);
					com.houtai.authority.Authority authority = null;
					// 如果方法和类声明中同时存在这个注解，那么方法中的会覆盖类中的设定。
					if (isMethondAnnotation) {
						authority = m.getAnnotation(com.houtai.authority.Authority.class);
					} else if (isClzAnnotation) {
						authority = clazz.getAnnotation(com.houtai.authority.Authority.class);
					}

					if (authority != null) {
						if (com.houtai.authority.AuthorityType.NoValidate == authority.value()) {
							return true;
						} else if (AuthorityType.Login == authority.value()) {
							// 验证登录
							if (request.getHeader("token") != null && !request.getHeader("token").equals("")) {
								//验证登录
								String token = request.getHeader("token");
								if (redisTemplate.opsForValue().get(token) == null) {
									responseMap.put("tip", "需要登录");
									responseMap.put("mark", 500);
									response.getWriter().write(responseMap.toString());
									return false;
								}
							} else {

								responseMap.put("tip", "需要登录");
								responseMap.put("mark", 500);
								response.getWriter().write(responseMap.toString());
								return false;
							}
						} else if (AuthorityType.User == authority.value()) {
							// 验证登录
							if (request.getHeader("token") != null && !request.getHeader("token").equals("")) {
								//验证登录
								String token = request.getHeader("token");
								if (redisTemplate.opsForValue().get(token) == null) {
									responseMap.put("tip", "需要登录");
									responseMap.put("mark", 500);
									response.getWriter().write(responseMap.toString());
									return false;
								} else {
									User user = (User)redisTemplate.opsForValue().get(token);
									if (user.getUser_type() == 1) {
										return true;
									} else {
										responseMap.put("tip", "必须是用户才可以访问");
										responseMap.put("mark", 501);
										response.getWriter().write(responseMap.toString());
										return false;
									}
								}
							} else {

								responseMap.put("tip", "需要登录");
								responseMap.put("mark", 500);
								response.getWriter().write(responseMap.toString());
								return false;
							}
						} else if (AuthorityType.UserReal == authority.value()) {
							// 验证登录
							if (request.getHeader("token") != null && !request.getHeader("token").equals("")) {
								//验证登录
								String token = request.getHeader("token");
								if (redisTemplate.opsForValue().get(token) == null) {
									responseMap.put("tip", "需要登录");
									responseMap.put("mark", 500);
									response.getWriter().write(responseMap.toString());
									return false;
								} else {
									User user = (User)redisTemplate.opsForValue().get(token);
									if (user.getUser_type() == 1 && user.getIs_auth() == 0) {
										return true;
									} else {
										responseMap.put("tip", "需要先进行实名认证");
										responseMap.put("mark", 502);
										response.getWriter().write(responseMap.toString());
										return false;
									}
								}
							} else {

								responseMap.put("tip", "需要登录");
								responseMap.put("mark", 500);
								response.getWriter().write(responseMap.toString());
								return false;
							}
						} else if (AuthorityType.CompanyReal == authority.value()) {
							// 验证登录
							if (request.getHeader("token") != null && !request.getHeader("token").equals("")) {
								//验证登录
								String token = request.getHeader("token");
								if (redisTemplate.opsForValue().get(token) == null) {
									responseMap.put("tip", "需要登录");
									responseMap.put("mark", 500);
									response.getWriter().write(responseMap.toString());
									return false;
								} else {
									User user = (User)redisTemplate.opsForValue().get(token);
									if (user.getUser_type() == 2 && user.getIs_auth() == 0) {
										return true;
									} else {
										responseMap.put("tip", "需要先进行企业认证");
										responseMap.put("mark", 502);
										response.getWriter().write(responseMap.toString());
										return false;
									}
								}
							} else {

								responseMap.put("tip", "需要登录");
								responseMap.put("mark", 500);
								response.getWriter().write(responseMap.toString());
								return false;
							}
						} else if (AuthorityType.Company == authority.value()) {
							// 验证登录
							if (request.getHeader("token") != null && !request.getHeader("token").equals("")) {
								//验证登录
								String token = request.getHeader("token");
								if (redisTemplate.opsForValue().get(token) == null) {
									responseMap.put("tip", "需要登录");
									responseMap.put("mark", 500);
									response.getWriter().write(responseMap.toString());
									return false;
								} else {
									User user = (User)redisTemplate.opsForValue().get(token);
									if (user.getUser_type() == 2 ) {
										return true;
									} else {
										responseMap.put("tip", "必须是企业用户才可访问");
										responseMap.put("mark", 502);
										response.getWriter().write(responseMap.toString());
										return false;
									}
								}
							} else {

								responseMap.put("tip", "需要登录");
								responseMap.put("mark", 500);
								response.getWriter().write(responseMap.toString());
								return false;
							}
						}
					}
					return true;
				}
			} catch (Exception e) {
				logger.info(e.toString());
			}
		}
		return false;
	}

	public void setRedisTemplate(RedisTemplate redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

}
